package adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lxh.testinfo.R;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import database.LivesqliteUtil;
import jsonbean.Qualitys;

/**
 * Created by Lxh on 2018/1/2.
 */

public class LivePagerAdapter extends PagerAdapter {
    private List<View> listView;
    private Context context;
    private LinearLayout linearLayout;
    private GraphicalView graphicalView;
    private List<Double> listPM = new ArrayList<>();
    private List<Double> listCo = new ArrayList<>();
    private List<Double> listTemp = new ArrayList<>();
    private List<Double> listLight = new ArrayList<>();
    private List<Double> listHum = new ArrayList<>();
    private TextView m_showText;

    public LivePagerAdapter(List<View> listView, Context context) {
        this.listView = listView;
        this.context = context;
        LivesqliteUtil util = new LivesqliteUtil(context);
        List<Qualitys> listAll = util.QueryData();
        for (Qualitys qualitys : listAll) {
            listCo.add((double) qualitys.getCo2());
            listHum.add((double) qualitys.getHumidity());
            listLight.add((double) qualitys.getLight());
            listPM.add((double) qualitys.getPm());
            listTemp.add((double) qualitys.getTemp());
        }
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(listView.get(position));
        View view = listView.get(position);
        linearLayout = view.findViewById(R.id.item_viewpager);
        m_showText = view.findViewById(R.id.show_text);
        switch (position) {
            case 0:
                // 柱状图
                XYMultipleSeriesDataset dataset = getDataSet();
                XYMultipleSeriesRenderer renderer = setRender();
                graphicalView = ChartFactory.getBarChartView(view.getContext(), dataset, renderer, BarChart.Type.DEFAULT);
                linearLayout.addView(graphicalView);
                break;
            case 1:
                // 折线图
                XYMultipleSeriesRenderer renderer_temp = setRender_temp();
                XYMultipleSeriesDataset dataset_temp = getDataSet_temp();
                graphicalView = ChartFactory.getLineChartView(view.getContext(), dataset_temp, renderer_temp);
                linearLayout.addView(graphicalView);
                break;
            case 2:
                // 折线图
                XYMultipleSeriesRenderer renderer_Hum = setRender_Hum();
                XYMultipleSeriesDataset dataset_Hum = getDataSet_Hum();
                graphicalView = ChartFactory.getLineChartView(view.getContext(), dataset_Hum, renderer_Hum);
                linearLayout.addView(graphicalView);
                break;
            case 3:
                // 折线图
                XYMultipleSeriesRenderer renderer_co = setRender_Co();
                XYMultipleSeriesDataset dataset_co = getDataSet_CO();
                graphicalView = ChartFactory.getLineChartView(view.getContext(), dataset_co, renderer_co);
                linearLayout.addView(graphicalView);
                break;
        }
        return listView.get(position);
    }

    private XYMultipleSeriesRenderer setRender_Co() {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setMargins(new int[]{0, 80, 0, 20});
        renderer.setAxisTitleTextSize(20f);
        renderer.setXAxisMin(0.5);
        renderer.setXAxisMax(25.0);
        renderer.setYAxisMin(200);
        renderer.setYAxisMax(800);
        renderer.setDisplayValues(false);
        renderer.setShowAxes(false);

        renderer.setApplyBackgroundColor(true);
        renderer.setBackgroundColor(Color.parseColor("#52c6f7"));
        renderer.setLabelsColor(0xFFFFFF);
        renderer.setZoomButtonsVisible(false);
        renderer.setZoomInLimitX(7);
        renderer.setZoomEnabled(false);
        renderer.setXLabelsAlign(Paint.Align.CENTER);
        renderer.setYLabelsAlign(Paint.Align.LEFT);
        renderer.setYLabels(4);
        renderer.setXLabels(0);

        renderer.addXTextLabel(1, "03");
        renderer.addXTextLabel(2, "06");
        renderer.addXTextLabel(3, "09");
        renderer.addXTextLabel(4, "12");
        renderer.addXTextLabel(5, "15");
        renderer.addXTextLabel(6, "18");
        renderer.addXTextLabel(7, "21");
        renderer.addXTextLabel(8, "24");
        renderer.addXTextLabel(9, "27");
        renderer.addXTextLabel(10, "30");
        renderer.addXTextLabel(11, "33");
        renderer.addXTextLabel(12, "36");
        renderer.addXTextLabel(13, "39");
        renderer.addXTextLabel(14, "42");
        renderer.addXTextLabel(15, "45");
        renderer.addXTextLabel(16, "48");
        renderer.addXTextLabel(17, "51");
        renderer.addXTextLabel(18, "54");
        renderer.addXTextLabel(19, "57");
        renderer.addXTextLabel(20, "60");

        renderer.setLabelsColor(Color.WHITE);
        renderer.setLabelsTextSize(20f);
        renderer.setYLabelsColor(0, Color.WHITE);
        renderer.setPointSize(18f);
        renderer.setMarginsColor(Color.parseColor("#52c6f7"));
        renderer.setClickEnabled(false);
        renderer.setShowGridX(true);
        renderer.setGridColor(Color.WHITE);

        XYSeriesRenderer xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setPointStyle(PointStyle.CIRCLE);
        xySeriesRenderer.setPointStrokeWidth(3f);
        xySeriesRenderer.setColor(Color.WHITE);
        xySeriesRenderer.setFillPoints(true);
        xySeriesRenderer.setLineWidth(10.0f);

        renderer.addSeriesRenderer(xySeriesRenderer);
        return renderer;
    }


    public XYMultipleSeriesDataset getDataSet_CO() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        XYSeries xySeries = new XYSeries("二氧化碳");
        for (int i = 0; i < listCo.size(); i++) {
            xySeries.add(i + 1, listCo.get(i));
        }
        Double max = Collections.max(listCo);
        m_showText.setText("过去一分钟最大相对浓度：" + max);
        dataset.addSeries(xySeries);
        return dataset;
    }

    private XYMultipleSeriesRenderer setRender_Hum() {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setMargins(new int[]{0, 80, 0, 20});
        renderer.setAxisTitleTextSize(20);
        renderer.setYAxisMax(90);
        renderer.setYAxisMin(20);
        renderer.setXAxisMax(25.0);
        renderer.setXAxisMin(0.5);

        renderer.setShowGrid(true);
        renderer.setGridColor(Color.BLACK);
        renderer.setDisplayValues(false);
        renderer.setShowAxes(false);
        renderer.setApplyBackgroundColor(true);
        renderer.setBackgroundColor(Color.WHITE);
        renderer.setLabelsColor(0x000000);

        renderer.setZoomEnabled(false, false);
        renderer.setZoomButtonsVisible(false);
        renderer.setZoomInLimitX(7);
        renderer.setXLabelsAlign(Paint.Align.CENTER);
        renderer.setYLabelsAlign(Paint.Align.LEFT);
        renderer.setXLabels(0);
        renderer.setYLabels(6);
        renderer.addXTextLabel(1, "03");
        renderer.addXTextLabel(2, "06");
        renderer.addXTextLabel(3, "09");
        renderer.addXTextLabel(4, "12");
        renderer.addXTextLabel(5, "15");
        renderer.addXTextLabel(6, "18");
        renderer.addXTextLabel(7, "21");
        renderer.addXTextLabel(8, "24");
        renderer.addXTextLabel(9, "27");
        renderer.addXTextLabel(10, "30");
        renderer.addXTextLabel(11, "33");
        renderer.addXTextLabel(12, "36");
        renderer.addXTextLabel(13, "39");
        renderer.addXTextLabel(14, "42");
        renderer.addXTextLabel(15, "45");
        renderer.addXTextLabel(16, "48");
        renderer.addXTextLabel(17, "51");
        renderer.addXTextLabel(18, "54");
        renderer.addXTextLabel(19, "57");
        renderer.addXTextLabel(20, "60");

        renderer.setXLabelsColor(Color.BLACK);
        renderer.setLabelsTextSize(20);
        renderer.setYLabelsColor(0, Color.BLACK);
        renderer.setPointSize(12f);
        renderer.setMarginsColor(Color.WHITE);
        renderer.setClickEnabled(false);

        XYSeriesRenderer xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setPointStyle(PointStyle.CIRCLE);
        xySeriesRenderer.setPointStrokeWidth(3f);
        xySeriesRenderer.setColor(Color.GRAY);
        xySeriesRenderer.setFillPoints(true);
        xySeriesRenderer.setDisplayChartValues(false);
        xySeriesRenderer.setLineWidth(4f);

        renderer.addSeriesRenderer(xySeriesRenderer);
        return renderer;
    }

    public XYMultipleSeriesDataset getDataSet_Hum() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        XYSeries xySeries = new XYSeries("相对湿度");
        for (int i = 0; i < listHum.size(); i++) {
            xySeries.add(i + 1, listHum.get(i));
        }
        Double max = Collections.max(listHum);
        m_showText.setText("过去一分钟最大相对湿度：" + max + "%");
        dataset.addSeries(xySeries);
        return dataset;
    }

    public XYMultipleSeriesRenderer setRender_temp() {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setMargins(new int[]{0, 80, 0, 20});    //设置外边距   上左下右
        renderer.setAxisTitleTextSize(20);              //设置坐标轴标题的大小
        renderer.setYAxisMin(0);            //y轴的最小值
        renderer.setYAxisMax(40);       //y轴的最大值
        renderer.setXAxisMin(0.5);        // x轴的最小值
        renderer.setXAxisMax(25.5);        // x轴的最大值
        renderer.setShowGridX(true);        // 显示X轴的 网格
        renderer.setGridColor(Color.BLACK);//设置网格的颜色

        renderer.setDisplayValues(false);// 不显示每个点的值
        renderer.setShowAxes(false);       //不显示坐标轴
        renderer.setApplyBackgroundColor(true); //是否显示背景颜色
        renderer.setBackgroundColor(Color.WHITE); //设置背景颜色
        renderer.setLabelsColor(0X000000);         //设置x y轴坐标轴值

        renderer.setZoomEnabled(false);         //是否可以 左右 上下 拉动
        renderer.setZoomButtonsVisible(false);      //是否显示 缩小 放大的btn
        renderer.setZoomInLimitX(7);
        renderer.setPanEnabled(false);

        renderer.setXLabelsAlign(Paint.Align.CENTER);  //x轴的值 显示到 居中位置
        renderer.setYLabelsAlign(Paint.Align.LEFT);     // y轴的值显示到 左侧位置
        renderer.setYLabels(7);     //y 轴显示的 值 的数量
        renderer.setXLabels(0);        // x 轴显示的 值的数量
        renderer.addXTextLabel(1, "03");
        renderer.addXTextLabel(2, "06");
        renderer.addXTextLabel(3, "09");
        renderer.addXTextLabel(4, "12");
        renderer.addXTextLabel(5, "15");
        renderer.addXTextLabel(6, "18");
        renderer.addXTextLabel(7, "21");
        renderer.addXTextLabel(8, "24");
        renderer.addXTextLabel(9, "27");
        renderer.addXTextLabel(10, "30");
        renderer.addXTextLabel(11, "33");
        renderer.addXTextLabel(12, "36");
        renderer.addXTextLabel(13, "39");
        renderer.addXTextLabel(14, "42");
        renderer.addXTextLabel(15, "45");
        renderer.addXTextLabel(16, "48");
        renderer.addXTextLabel(17, "51");
        renderer.addXTextLabel(18, "54");
        renderer.addXTextLabel(19, "57");
        renderer.addXTextLabel(20, "60");

        renderer.setXLabelsColor(Color.BLACK);
        renderer.setLabelsTextSize(20);
        renderer.setYLabelsColor(0, Color.BLUE);
        renderer.setPointSize(12f);
        renderer.setMarginsColor(Color.WHITE);
        renderer.setClickEnabled(false);

        XYSeriesRenderer xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setPointStyle(PointStyle.CIRCLE);
        xySeriesRenderer.setPointStrokeWidth(3);
        xySeriesRenderer.setColor(Color.GRAY);
        xySeriesRenderer.setFillPoints(true);
        xySeriesRenderer.setDisplayChartValues(false);
        xySeriesRenderer.setChartValuesTextSize(15);
        xySeriesRenderer.setLineWidth(4.0f);
        renderer.addSeriesRenderer(xySeriesRenderer);
        return renderer;
    }

    public XYMultipleSeriesDataset getDataSet_temp() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        XYSeries xySeries = new XYSeries("温度");
        for (int i = 0; i < listTemp.size(); i++) {
            xySeries.add(i + 1, listTemp.get(i));
        }
        Double max = Collections.max(listTemp);
        Double min = Collections.min(listTemp);
        m_showText.setText("过去一分钟最高气温：" + max + ",最低气温：" + min);
        dataset.addSeries(xySeries);
        return dataset;
    }

    public XYMultipleSeriesRenderer setRender() {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        SimpleSeriesRenderer r = new SimpleSeriesRenderer();
        r.setColor(Color.parseColor("#979797"));
        renderer.addSeriesRenderer(r);

        renderer.setZoomButtonsVisible(false);
        renderer.setZoomEnabled(false);
        renderer.setZoomInLimitX(7);
        renderer.setPanEnabled(false, false); //设置 左右滑动 ， 不许上下滑动
        renderer.setShowAxes(false);// 是否显示 坐标轴
        renderer.setYLabelsColor(0, Color.BLACK); //Y轴标签的颜色

        renderer.setLabelsTextSize(18);  //设置标签的字体大小
        renderer.setApplyBackgroundColor(true);//设置是否显示背景颜色
        renderer.setBackgroundColor(Color.WHITE);//设置背景颜色
        renderer.setMarginsColor(Color.WHITE);//设置图标周围的颜色
        renderer.setClickEnabled(true);//设置是否可点击


        renderer.setShowGridX(true); //是否显示网格  X
        renderer.setGridColor(Color.parseColor("#979797")); //设置网格颜色
        renderer.setBarSpacing(1.0); //柱子的粗细
        renderer.setYLabels(6);  //  X  Y要显示的标签
        renderer.setXLabels(0);
        renderer.addXTextLabel(1, "03");
        renderer.addXTextLabel(2, "06");
        renderer.addXTextLabel(3, "09");
        renderer.addXTextLabel(4, "12");
        renderer.addXTextLabel(5, "15");
        renderer.addXTextLabel(6, "18");
        renderer.addXTextLabel(7, "21");
        renderer.addXTextLabel(8, "24");
        renderer.addXTextLabel(9, "27");
        renderer.addXTextLabel(10, "30");

        renderer.addXTextLabel(11, "33");
        renderer.addXTextLabel(12, "36");
        renderer.addXTextLabel(13, "39");
        renderer.addXTextLabel(14, "42");
        renderer.addXTextLabel(15, "45");
        renderer.addXTextLabel(16, "48");
        renderer.addXTextLabel(17, "51");
        renderer.addXTextLabel(18, "54");
        renderer.addXTextLabel(19, "57");
        renderer.addXTextLabel(20, "60");

        renderer.setXLabelsColor(Color.BLACK);
        renderer.setMargins(new int[]{0, 80, 0, 20}); //设置外边距   上左下右
        renderer.setXTitle("x轴");
        renderer.setYTitle("Y轴");
        renderer.setXAxisMin(0.5);
        renderer.setXAxisMax(25.0);
        renderer.setYAxisMin(0);
        renderer.setYAxisMax(210);
        return renderer;
    }

    public XYMultipleSeriesDataset getDataSet() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        CategorySeries series = new CategorySeries("空气质量");
        for (Double pm : listPM) {
            Log.w("Lxh", "getDataSet: " + pm);
            series.add(pm.doubleValue());
        }
        Double max = Collections.max(listPM);
        m_showText.setText("过去一分钟空气质量最差值：" + max.doubleValue());
        dataset.addSeries(series.toXYSeries());
        return dataset;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(listView.get(position));
    }

    @Override
    public int getCount() {
        return listView.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}
